<?php
/**
 * 测试商品分类编辑功能
 */

// 模拟HTTP请求函数
function mockRequest($method, $url, $data = null) {
    echo "=== 测试 $method 请求到 $url ===\n";
    
    if ($data) {
        echo "请求数据: " . json_encode($data, JSON_UNESCAPED_UNICODE) . "\n";
    }
    
    // 模拟响应
    if ($method === 'POST' && $url === '/admin/goods-cat/update') {
        if (empty($data['id'])) {
            return [
                'code' => 0,
                'msg' => '分类ID不能为空',
                'data' => null
            ];
        }
        
        if (empty($data['name'])) {
            return [
                'code' => 0,
                'msg' => '分类名称不能为空',
                'data' => null
            ];
        }
        
        // 模拟成功响应
        return [
            'code' => 20000,
            'msg' => '更新成功',
            'data' => [
                'id' => $data['id'],
                'name' => $data['name'],
                'fid' => $data['parent_id'] ?? 0,
                'sort' => $data['sort'] ?? 0,
                'image' => $data['image'] ?? '',
                'created_at' => time(),
                'updated_at' => time()
            ]
        ];
    }
    
    return [
        'code' => 0,
        'msg' => '未知的请求',
        'data' => null
    ];
}

// 测试编辑功能
echo "开始测试商品分类编辑功能...\n\n";

// 测试1: 正常编辑
$updateData = [
    'id' => 1,
    'name' => '电子产品',
    'parent_id' => 0,
    'sort' => 10,
    'image' => '/uploads/cat/electronics.jpg'
];

$response = mockRequest('POST', '/admin/goods-cat/update', $updateData);
echo "响应: " . json_encode($response, JSON_UNESCAPED_UNICODE) . "\n\n";

// 测试2: 缺少ID
$updateData2 = [
    'name' => '电子产品',
    'parent_id' => 0,
    'sort' => 10
];

$response2 = mockRequest('POST', '/admin/goods-cat/update', $updateData2);
echo "响应: " . json_encode($response2, JSON_UNESCAPED_UNICODE) . "\n\n";

// 测试3: 缺少名称
$updateData3 = [
    'id' => 1,
    'parent_id' => 0,
    'sort' => 10
];

$response3 = mockRequest('POST', '/admin/goods-cat/update', $updateData3);
echo "响应: " . json_encode($response3, JSON_UNESCAPED_UNICODE) . "\n\n";

echo "测试完成！\n";
?>
